package com.lb.shopguide.ui.fragment.guide.pay;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.lb.baselib.http.ResponseCode;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.AlipayAccountBean;
import com.lb.shopguide.event.guide.AlipayAccountChangeEvent;
import com.lb.shopguide.event.guide.BindAlipayEvent;
import com.lb.shopguide.event.guide.StartBrotherEvent;
import com.lb.shopguide.http.method.ApiMethodGuide;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.baselib.http.SimpleObserverOtherListener;
import com.lb.shopguide.ui.dialog.DialogSetSuccess;
import com.lb.shopguide.ui.view.NormalTitleBar;
import com.lb.shopguide.util.lb.BizUtil;
import com.trello.rxlifecycle2.android.FragmentEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.RequestBody;

/**
 * Created by kang on 2017/9/6.
 */

public class FragmentSetAlipayAccount extends BaseCommonFragment {
    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.et_alipay_account)
    EditText etAlipayAccount;
    @BindView(R.id.et_real_name)
    EditText etRealname;
    @BindView(R.id.btn_complete)
    TextView tvComplete;

    private int type;
    private AlipayAccountBean accountBean;

    public static FragmentSetAlipayAccount newInstance(int type) {
        FragmentSetAlipayAccount fragment = new FragmentSetAlipayAccount();
        Bundle bundle = new Bundle();
        bundle.putInt(AppConstant.ALIPAY_COUNT_TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    public static FragmentSetAlipayAccount newInstance(int type, AlipayAccountBean bean) {
        FragmentSetAlipayAccount fragment = new FragmentSetAlipayAccount();
        Bundle bundle = new Bundle();
        bundle.putInt(AppConstant.ALIPAY_COUNT_TYPE, type);
        bundle.putSerializable(AppConstant.ALIPAY_ACCOUNT_BEAN, bean);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_set_alipay_account;
    }

    @Override
    public void initData() {
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.ALIPAY_COUNT_TYPE)) {
            type = bundle.getInt(AppConstant.ALIPAY_COUNT_TYPE);
        }
        if (bundle.containsKey(AppConstant.ALIPAY_ACCOUNT_BEAN)) {
            accountBean = (AlipayAccountBean) bundle.getSerializable(AppConstant.ALIPAY_ACCOUNT_BEAN);
        }
    }

    @Override
    public void initView() {

        if (type == AppConstant.SET_ALIPAY_ACCOUNT) {
            ntb.setTitleText("设置账户");
            tvComplete.setText("完成");
        } else if (type == AppConstant.RESET_ALIPAY_ACCOUNT) {
            ntb.setTitleText("重置账户");
            tvComplete.setText("立即重置");
            etAlipayAccount.setText("");
            etRealname.setText("");
        }
        ntb.setLeftImageSrc(R.drawable.iv_back);
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {

    }

    @Override
    public void requestError(String error) {

    }

    @Override
    public String getPageTag() {
        return FragmentSetAlipayAccount.class.getName();
    }


    private void saveAlipayAccountInfo(String receiveAccount, String receiveTrueName) {
        HashMap hashMap = new HashMap();
        hashMap.put("receiveAccount", receiveAccount);
        hashMap.put("receiveTrueName", receiveTrueName);
        RequestBody body = BizUtil.getBodyFromMap(hashMap);
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == (ResponseCode.RESPONSE_SUCCESS)) {
//                        ToastUtils.showShort("保存成功");
                    EventBus.getDefault().post(new AlipayAccountChangeEvent());
                    DialogSetSuccess dialogSetSuccess = DialogSetSuccess.getDialogSetSuccess();
                    dialogSetSuccess.show(getFragmentManager(), getPageTag());
                    dialogSetSuccess.setOnDialogClickListener(new DialogSetSuccess.OnDialogClickListener() {
                        @Override
                        public void onSure() {
                            pop();
                            EventBus.getDefault().post(new BindAlipayEvent());
                        }
                    });
                } else {
                    ToastUtils.showShort(response.getMessage());
                }
            }
        };
        SimpleObserverOtherListener errorListener = new SimpleObserverOtherListener() {
            @Override
            public void onSessionOutOfDate() {

            }

            @Override
            public void onError(String error) {
                ToastUtils.showShort("保存失败" + error);
            }
        };
        ApiMethodGuide.saveAlipayAccountInfo(new SimpleLbObserver(listener, errorListener), body, this, FragmentEvent.DESTROY_VIEW);
    }

    @OnClick(R.id.image_left)
    void leftClick() {
        pop();
    }

    @OnClick(R.id.btn_complete)
    void completeClick() {
        String account = etAlipayAccount.getText().toString();
        String realname = etRealname.getText().toString();
        if (TextUtils.isEmpty(account)) {
            ToastUtils.showShort("请输入支付宝账号");
            return;
        }
        if (TextUtils.isEmpty(realname)) {
            ToastUtils.showShort("请输入真实姓名");
            return;
        }
        saveAlipayAccountInfo(account, realname);
    }
}
